Imports DevExpress.Utils.Drawing
Imports DevExpress.XtraEditors
Imports DevExpress.XtraBars.Ribbon
Imports DevExpress.XtraBars
Imports DevExpress.Skins

Public Class FrmInicial

    Private Sub FrmInicial_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        On Error Resume Next

        GuardarArchivoLayout()
        GuardarSkin()
        Application.Exit()
    End Sub

    Private Sub FrmInicial_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try

            CargarArchivoLayout()
            RecuperarSkin()

            'Skin
            For Each skin As SkinContainer In SkinManager.Default.Skins
                Dim item As BarCheckItem = RibbonGeneral.Items.CreateCheckItem(skin.SkinName, False)
                item.Tag = skin.SkinName
                AddHandler item.ItemClick, AddressOf OnPaintStyleClick
            Next skin

            RibbonGeneral.SelectedPage = RbPagRegistros
            Text = My.Application.Info.ProductName
            LblUsuarioNombre.Caption = UsuarioActual.Nombre

        Catch ex As SqlException
            DevExpress.XtraEditors.XtraMessageBox.Show(String.Format(My.Resources.ResMensajes.ErrorAlRecuperarNombreUsuario, ex.Message), My.Application.Info.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
            Application.Exit()
        Catch ex As ReglaNegociosMetodosException
            DevExpress.XtraEditors.XtraMessageBox.Show(String.Format(My.Resources.ResMensajes.ErrorAlRecuperarNombreUsuario, ex.Message), My.Application.Info.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
            Application.Exit()
        Catch ex As Exception
            DevExpress.XtraEditors.XtraMessageBox.Show(String.Format(My.Resources.ResMensajes.ErrorAlRecuperarSkin, Environment.NewLine, ex.Message), My.Application.Info.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

#Region "SkinGallery"

    Private Sub OnPaintStyleClick(ByVal sender As Object, ByVal e As ItemClickEventArgs)
        DefaultLookAndFeel1.LookAndFeel.SetSkinStyle(e.Item.Tag.ToString())
    End Sub

    Private Sub InitSkinGallery()
        Using imageButton As SimpleButton = New SimpleButton()
            For Each cnt As SkinContainer In SkinManager.Default.Skins
                imageButton.LookAndFeel.SetSkinStyle(cnt.SkinName)
                Dim gItem As GalleryItem = New GalleryItem()
                Dim groupIndex As Integer = 0
                If cnt.SkinName.IndexOf("Office") > -1 Then
                    groupIndex = 1
                End If
                rgbiSkins.Gallery.Groups(groupIndex).Items.Add(gItem)
                gItem.Caption = cnt.SkinName
                gItem.Image = GetSkinImage(imageButton, 32, 17, 2)
                gItem.HoverImage = GetSkinImage(imageButton, 70, 36, 5)
                gItem.Caption = cnt.SkinName
                gItem.Hint = cnt.SkinName
                rgbiSkins.Gallery.Groups(1).Visible = False
            Next
        End Using
    End Sub

    Private Shared Function GetSkinImage(ByVal button As SimpleButton, ByVal width As Integer, ByVal height As Integer, ByVal indent As Integer) As Bitmap
        Dim image As Bitmap = New Bitmap(width, height)
        Using g As Graphics = Graphics.FromImage(image)
            Dim info As StyleObjectInfoArgs = New StyleObjectInfoArgs(New GraphicsCache(g)) With {.Bounds = New Rectangle(0, 0, width, height)}
            button.LookAndFeel.Painter.GroupPanel.DrawObject(info)
            button.LookAndFeel.Painter.Border.DrawObject(info)
            info.Bounds = New Rectangle(indent, indent, width - indent * 2, height - indent * 2)
            button.LookAndFeel.Painter.Button.DrawObject(info)
        End Using
        Return image
    End Function

    Private Sub rgbiSkins_Gallery_ItemClick(ByVal sender As Object, ByVal e As GalleryItemClickEventArgs) Handles rgbiSkins.Gallery.ItemClick
        DevExpress.LookAndFeel.UserLookAndFeel.Default.SetSkinStyle(e.Item.Caption)
    End Sub

    Private Sub rgbiSkins_Gallery_InitDropDownGallery(ByVal sender As Object, ByVal e As InplaceGalleryEventArgs) Handles rgbiSkins.Gallery.InitDropDownGallery
        e.PopupGallery.CreateFrom(rgbiSkins.Gallery)
        e.PopupGallery.AllowFilter = False
        e.PopupGallery.ShowItemText = True
        e.PopupGallery.ShowGroupCaption = True
        e.PopupGallery.AllowHoverImages = False
        For Each galleryGroup As GalleryItemGroup In e.PopupGallery.Groups
            For Each item As GalleryItem In galleryGroup.Items
                item.Image = item.HoverImage
            Next item
        Next galleryGroup
        e.PopupGallery.ColumnCount = 2
        e.PopupGallery.ImageSize = New Size(70, 36)
    End Sub

    Private Shared Sub GuardarSkin()
        Try
            My.Settings.LookAndFeelSetting = DevExpress.LookAndFeel.UserLookAndFeel.Default.ActiveSkinName
            My.Settings.Save()
        Catch ex As Exception
            DevExpress.XtraEditors.XtraMessageBox.Show(String.Format(My.Resources.ResMensajes.ErrorAlRecuperarSkin, Environment.NewLine, ex.Message), My.Application.Info.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End Sub

    Public Shared Sub RecuperarSkin()
        Try
            If My.Settings.LookAndFeelSetting = "" Then
                GuardarSkin()
            Else
                DevExpress.LookAndFeel.UserLookAndFeel.Default.SetSkinStyle(My.Settings.LookAndFeelSetting)
            End If
        Catch ex As Exception
            DevExpress.XtraEditors.XtraMessageBox.Show(String.Format(My.Resources.ResMensajes.ErrorAlRecuperarSkin, Environment.NewLine, ex.Message), My.Application.Info.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

#Region "Layout (Guardar Layout Y Cargar Layout)"
    Private Sub GuardarArchivoLayout()
        Dim FileName As String = String.Format("{0}\{1}.xml", Application.StartupPath, Name)
        Try
            RibbonGeneral.Manager.SaveLayoutToXml(FileName)
        Catch ex As Exception
            DevExpress.XtraEditors.XtraMessageBox.Show(String.Format(My.Resources.ResMensajes.ErrorGuardarConfiguraciones, ex.Message), My.Application.Info.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub CargarArchivoLayout()
        Dim FileName As String = String.Format("{0}\{1}.xml", Application.StartupPath, Name)
        Try
            If System.IO.File.Exists(FileName) Then
                RibbonGeneral.Manager.RestoreLayoutFromXml(FileName)
            End If
        Catch ex As Exception
            DevExpress.XtraEditors.XtraMessageBox.Show(String.Format(My.Resources.ResMensajes.ErrorGuardarConfiguraciones, ex.Message), My.Application.Info.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub
#End Region

#End Region

#Region "Grabar Auditoria"
    'TODO:
#End Region

    Private Sub BtnFormularios_ItemClick(ByVal sender As System.Object, ByVal e As ItemClickEventArgs) Handles BtnFormularios.ItemClick
        try
            If FormularioAbierto(RolObj.FormularioText, Me) = True Then
                Exit Sub
            End If
            Dim Frm As New FrmDosCampos(ClaseEnumObj.Roles.Formularios) With {.MdiParent = Me}
            If Not Frm.Permisos.Visualizar Then
                DevExpress.XtraEditors.XtraMessageBox.Show(My.Resources.ResMensajes.PermisoDenegado, My.Application.Info.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Hand)
                Frm.Dispose()
            Else
                Frm.Show()
            End If

        Catch ex As SqlException
            DevExpress.XtraEditors.XtraMessageBox.Show(String.Format(My.Resources.ResMensajes.ErrorAlAbrirFormulario, ex.Message), My.Application.Info.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Catch ex As ReglaNegociosMetodosException
            DevExpress.XtraEditors.XtraMessageBox.Show(String.Format(My.Resources.ResMensajes.ErrorAlAbrirFormulario, ex.Message), My.Application.Info.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Catch ex As Exception
            DevExpress.XtraEditors.XtraMessageBox.Show(String.Format(My.Resources.ResMensajes.ErrorAlRecuperarSkin, Environment.NewLine, ex.Message), My.Application.Info.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub TmrHora_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TmrHora.Tick
        LblHora.Caption = Now
    End Sub
End Class